<?php
/**
 * The MIT License
 *
 * Copyright (c) <year> <copyright holders>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @author Damiano Ciarla
 * @package plugin
 * @subpackage entity_manager/class/config
 */
class EntityManager_Config
{
	private $_folderModel = "model";

	private $_connectionName = "plugin_entity_manager";
	private $_custom = false;
	private $_host;
	private $_username;
	private $_password;
	private $_dbname;

	/**
	 * Istanza della libreria per la gestione della connessione al database
	 *
	 * @var BXDDb
	 */
	private $_BXDDb;

	public function __construct()
	{
		spl_autoload_register(array($this, "_autoloadModel"));
	}

	/**
	 * Metodo che imposta o restituisce il nome della cartella dove sono contenute
	 * le entity generate
	 *
	 * @param string $folderName Nome della cartella dove sono contenute le entity
	 *
	 * @return string
	 */
	public function folderModel($folderName = null)
	{
		if($folderName === null)
			return $this->_folderModel;

		$this->_folderModel = $folderName;
	}

	/**
	 * Metodo che comunica al plugin di usare una connessione precedentemente creata
	 * impostandola come predefinita del progetto
	 *
	 * @param string $connectionName Nome della connessione
	 */
	public function useKnownConnection($connectionName)
	{
		$this->_connectionName = $connectionName;
		$this->_custom = false;
		$this->_host = null;
		$this->_username = null;
		$this->_password = null;
		$this->_dbname = null;
	}

	/**
	 * Metodo che comunica al plugin di creare una nuova connessione impostandola
	 * come predefinita del progetto
	 *
	 * @param string $host Host dove risiede il database
	 * @param string $username Username per accedere al database
	 * @param string $password Password per accedere al database
	 * @param string $dbname Nome del database
	 * @param string $connectionName Nome della connessione
	 */
	public function useCustomConnection($host, $username, $password, $dbname, $connectionName = null)
	{
		if($connectionName !== null)
			$this->_connectionName = $connectionName;

		$this->_custom = true;
		$this->_host = $host;
		$this->_username = $username;
		$this->_password = $password;
		$this->_dbname = $dbname;
	}

	/**
	 * Metodo che ritorna l'istanza della libreria per la gestione del database
	 *
	 * @return BXDDb
	 */
	public function getConnection()
	{
		if($this->_BXDDb === null)
		{
			if($this->_custom)
			{
				Bxd::includeLib()->db();
				Bxd::includeLib()->dbConfig();

				$dbConf = new BXDDbConfig();
				$dbConf->connectionName	= $this->_connectionName;
				$dbConf->host = $this->_host;
				$dbConf->username = $this->_username;
				$dbConf->passwd = $this->_password;
				$dbConf->dbname = $this->_dbname;
				$dbConf->type = "mysql";
				$dbConf->persistent = false;
				$dbConf->collate = "utf8_unicode_ci";
				$dbConf->charset = "utf8";

				$this->_BXDDb = new BXDDb($dbConf);
			}
			else
			{
				$this->_BXDDb = BXDDb::getInstance();
			}

			$this->_BXDDb->useConnection($this->_connectionName);
		}

		return $this->_BXDDb;
	}

	private function _autoloadModel($className)
	{
		if(strpos($className, "Base") === false)
		{
			$classPath = Bxd::getInstance()->pathApplication . '/' . $this->_folderModel . '/' . $className . '.php';

			if(!file_exists($classPath))
				throw new EntityManager_Exception(array($className, $folder), EntityManager_Exception::LOAD_ENTITY_EXTENDED_NOT_FOUND);

			include_once $classPath;
		}
		else
		{
			$classPath = Bxd::getInstance()->pathApplication . '/' . $this->_folderModel . '/base/' . $className . '.php';

			if(!file_exists($classPath))
				throw new EntityManager_Exception(array($className, $folder), EntityManager_Exception::LOAD_ENTITY_EXTENDED_NOT_FOUND);

			include_once $classPath;
		}
	}
}
?>
